<template>
  <div>
    <h1>企业信息</h1>
    <el-table :data="entrOnelist" border>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="password" label="密码"></el-table-column>
      <el-table-column prop="name" label="企业名称"></el-table-column>
      <el-table-column prop="email" label="电子邮箱"></el-table-column>
      <el-table-column prop="phone" label="联系电话"></el-table-column>
      <el-table-column prop="manage" label="从事行业"></el-table-column>
      <el-table-column prop="address" label="企业地址"></el-table-column>
      <el-table-column prop="introduce" label="公司简介"></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="showEditDialog(scope.row.username)" type="text" size="small">编辑</el-button>
          <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='算了'
            icon="el-icon-info"
            icon-color="red"
            title="确定要删除这条内容吗？"
            @confirm=deleteInfo(scope.row.id)
          >
            <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    编辑-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialog">
      <el-form status-icon :model="editForm" ref="editFormRef"
               label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input show-password type="password" v-model="editForm.password"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input type="text" v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input type="text" v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input type="text" v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="从事行业" prop="manage">
          <el-input type="text" v-model="editForm.manage"></el-input>
        </el-form-item>
        <el-form-item label="企业地址" prop="address">
          <el-input type="text" v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="公司简介" prop="introduce">
          <el-input type="text" v-model="editForm.introduce"></el-input>
        </el-form-item>
      </el-form>
      <div class="register-button" slot="footer">
        <el-button @click="editDialog=false">取 消</el-button>
        <el-button type="primary" @click="editInfo('editFormRef')">提交修改</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
  import Qs from "qs";

  export default {
    name: "entrOneInfo",
    data() {
      return {
        entrOnelist: [],
        username: this.$store.state.username,
        //企业个人信息对象
        editForm: {},
        //企业个人信息编辑对话框开关
        editDialog: false,
      }
    },

    created() {
      this.getEntrOnelist();
    },

    methods: {
      async getEntrOnelist() {
        let _this = this;
        console.log(_this.username);
        await this.$axios.post('/api/enterprise/likeOneInfo', Qs.stringify({
          username: _this.username
        })).then((res) => {
          console.log(res);
          if (res.status !== 200) {
            return _this.$message.error("用户数据获取失败")
          }
          _this.entrOnelist = res.data.data
        })
      },


      // //展示编辑之前的信息
      async showEditDialog(userName) {
        console.log(userName);
        const {data: res} = await this.$axios.post("/api/enterprise/getOneInfo", Qs.stringify({username: userName}))
        console.log(res)
        if (res.code !== 200) {
          this.$message.error("查询用户信息失败")
        }
        this.editForm = res.data
        console.log(res.data)
        this.editDialog = true
      },

      //确认修改方法
      async editInfo(formName) {
        this.$refs[formName].validate(async valid => {
          if (!valid) return this.$message.error("参数错误");
          let _this = this;
          const {data: res} = await _this.$axios.put("/api/enterprise/editOneInfo/", Qs.stringify({
            id: _this.editForm.id,
            username: _this.editForm.username,
            password: _this.editForm.password,
            name: _this.editForm.name,
            email: _this.editForm.email,
            phone: _this.editForm.phone,
            manage: _this.editForm.manage,
            address: _this.editForm.address,
            introduce: _this.editForm.introduce,
          }))
          console.log(res)
          if (res.code !== 200) {
            return _this.$message.error("数据更新失败!");
          }
          _this.editDialog = false;
          _this.getEntrOnelist();
          _this.$message.success("数据更新成功!")
        })
      },
      //删除一条信息
      async deleteInfo(id) {
        let _this = this;
        console.log(id);
        const {data:res} = await _this.$axios.post("/api/recruitment/deleteInfo",Qs.stringify({id:id}))
        console.log(res)
        if(res.code!==200){
          return _this.$message.error(res.message);
        }
        this.getEntrOnelist();
        return _this.$message.success(res.message);
      },
    }
  }
</script>

<style scoped>

</style>
